package cn.xz.zysjz.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;
import cn.xz.zysjz.R;
import cn.xz.zysjz.adapter.KfListAdapter;
import cn.xz.zysjz.bean.AiBean;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.KfBean;
import cn.xz.zysjz.databinding.ActivityKfBinding;
import cn.xz.zysjz.utils.OkHttpUtils;

/**
 * 在线联系界面*/
public class KfActivity extends AppCompatActivity {

    //医生名字
    String mName;

    ActivityKfBinding mBinding;

    //列表适配器
    KfListAdapter mAdapter;

    //聊天内容
    ArrayList<KfBean> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_kf);
        if (Build.VERSION.SDK_INT >= 21) {
            this.getWindow()
                    .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        //获取哪个医生
        mName = getIntent().getStringExtra(CommonLib.JUMP_TYPE);
        initRV();
        initEvent();
        initData();
    }

    private void initData(){
        mBinding.tvTitle.setText(mName);
        //一开始模拟数据
        KfBean kfBean = new KfBean();
        kfBean.setType("0");
        kfBean.setValue("你好我是" + mName + "，请问有什么问题吗？");
        mList.add(kfBean);

        mAdapter.setList(mList);
    }

    private void initEvent(){
        //返回
        mBinding.viewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //点击发送
        mBinding.tvSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //输入框内容
                String mStr = mBinding.etKf.getText().toString();
                KfBean kfBean = new KfBean();
                kfBean.setType("1");
                kfBean.setValue(mStr);
                mAdapter.addData(kfBean);
                mBinding.etKf.setText("");
                speak(mStr);
            }
        });
    }

    //与AI交流
    private void speak(String str){
        //异步操作
        mBinding.tvSend.post(new Runnable() {
            @Override
            public void run() {
                //进行网络请求，给列表赋值
                OkHttpUtils.toGet("https://api.oioweb.cn/api/ai/chat?text=" + str, new OkHttpUtils.RequestSuccess<AiBean>() {
                    @Override
                    public void success(AiBean data) {
                        if (data != null){
                            //子线程进行UI操作
                            mBinding.tvSend.post(new Runnable() {
                                @Override
                                public void run() {
                                    AiBean.ResultBean result = data.getResult();
                                    if (result != null){
                                        String mStr = "稍等下，我正在忙";
                                        if (result.getText() != null){
                                            mStr = result.getText();
                                        }
                                        KfBean kfBean = new KfBean();
                                        kfBean.setType("0");
                                        kfBean.setValue(mStr);
                                        mAdapter.addData(kfBean);
                                        mBinding.etKf.setText("");
                                    }
                                }
                            });
                        }
                    }
                    @Override
                    public void error() {

                    }
                },AiBean.class);
            }
        });
    }

    private void initRV(){
        //列表相关数据
        LinearLayoutManager ms = new LinearLayoutManager(this);
        mAdapter = new KfListAdapter(R.layout.item_kf_list);
        mBinding.rvList.setLayoutManager(ms);
        mBinding.rvList.setAdapter(mAdapter);
    }
}